/*
 * @Description: 
 * @Author: Aroma
 * @Date: 2024-09-29 08:59:40
 * @LastEditors: Aroma
 * @LastEditTime: 2024-09-29 16:37:07
 * @别瞎看: 你个无脑牛马
 */

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
 * Test01Set
 */
public class Test01Set {

    public static void main(String[] args) {
        //实例化一个Set集合
        Set<String> set=new HashSet<String>();
        //添加元素
        set.add("hello1");
        set.add("hello2");
        set.add("hello3");
        set.add("hello4");
        set.add("hello5");
        set.add("hello5");
        set.add(null);
        set.add(null);
        System.out.println(set.size());
        //遍历查看元素
        for (String string : set) {
            //无顺序、不重复
            System.out.println(string);
        }
        //用迭代器遍历
        //创建迭代器对象
        Iterator<String> iterator = set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }
        //实例化一个set 集合，用于存放Student类的对象
        Set<Student> setStudent =new HashSet<>();
        //给集合添加元素
        setStudent.add(new Student("张三", 20));
        setStudent.add(new Student("张三", 20));
        setStudent.add(new Student("张三", 20));
        setStudent.add(new Student("张三", 20));
        System.out.println(setStudent.size());//4
        //所以 以上结果得知  HashSet认为这四个对象是不同的
        //因为获取对象的是hash值

        Student stu1=new Student("张三", 20);
        Student stu2=new Student("张三", 20);
        // 打印hash值
        System.out.println("stu1.hashCode()="+stu1.hashCode());
        System.out.println("stu2.hashCode()="+stu2.hashCode());
    }
}